package org.esisar.dao;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.esisar.model.Model;
import org.esisar.model.PictureOfAlbum;


public class PictureOfAlbumStatementCreator implements StatementCreator{

	private PreparedStatement preparedStatement;
	
	@Override
	public PreparedStatement addStatement(Connection connection, Model model) throws SQLException {
		
		PictureOfAlbum pictureOfAlbum = modelToPictureOfAlbum(model);
			
		preparedStatement = connection.prepareStatement("INSERT INTO pictureOfAlbum (Position,title,comments,idAlbum,idPicture) VALUES(?,?,?,?,?)");

		fillModel(pictureOfAlbum);
	
		return preparedStatement;
		
	}

	@Override
	public PreparedStatement deleteStatement(Connection connection, List<Long> keys) throws SQLException {

		preparedStatement = connection.prepareStatement("DELETE FROM pictureOfAlbum WHERE idAlbum = ? AND idPicture = ?");

		preparedStatement.setLong(1, keys.get(0));
		preparedStatement.setLong(2, keys.get(1));
				
		return preparedStatement;
	}

	@Override
	public PreparedStatement updateStatement(Connection connection, Model model)
			throws SQLException {
		
		PictureOfAlbum pictureOfAlbum = modelToPictureOfAlbum(model);
		
		preparedStatement = connection.prepareStatement("UPDATE pictureOfAlbum SET position = ?,title = ?, comments = ? WHERE idAlbum = ? AND idPicture = ?");

		fillModel(pictureOfAlbum);
		
		return preparedStatement;
	}

	private void fillModel(PictureOfAlbum pictureOfAlbum) throws SQLException {		
		preparedStatement.setString(1, pictureOfAlbum.getPosition());
		preparedStatement.setString(2, pictureOfAlbum.getTitle());
		preparedStatement.setString(3, pictureOfAlbum.getComments());
		preparedStatement.setString(4, pictureOfAlbum.getIdAlbum()+"");
		preparedStatement.setString(5, pictureOfAlbum.getIdPicture()+"");
	}

	private PictureOfAlbum modelToPictureOfAlbum(Model model) {
		PictureOfAlbum pictureOfAlbum;
		
		if (model instanceof PictureOfAlbum){
			pictureOfAlbum = (PictureOfAlbum) model;
		}else{
			throw new ClassCastException("Bad object instance");
		}
		return pictureOfAlbum;
	}
	
	@Override
	public PreparedStatement readStatement(Connection connection, String field,
			String value) throws SQLException {
		
		preparedStatement = connection.prepareStatement("select * from pictureofalbum where " + field + " = ? ");
		preparedStatement.setString(1, value);
		return preparedStatement;
		
	}

	@Override
	public Model fillModel(ResultSet rs) throws SQLException {
		String idAlbum = rs.getString("idAlbum");
		String idPicture = rs.getString("idPicture");
		String position = rs.getString("position");
		String title = rs.getString("title");
		String comments = rs.getString("comments");
		
		PictureOfAlbum pictureOfAlbum = new PictureOfAlbum(Long.parseLong(idAlbum), Long.parseLong(idPicture), position, title, comments);
		
		
		return pictureOfAlbum;
	}

}